package ldkxingzhe.l365calendar.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;

import ldkxingzhe.l365calendar.R;
import ldkxingzhe.l365calendar.fragment.GuideFragment;
import ldkxingzhe.l365calendar.utils.PreferenceHelper;

/**
 * 开始页面，　同时有引导页的功能
 * Created by ke on 16-1-14.
 */
public class Login extends BaseActivity{
    @SuppressWarnings("unused")
    private final String TAG = "Login";

    public static final String isNotShowGuideKeyName = "isNotShowGuide";

    FrameLayout fragmentLayout;
    View startView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        fragmentLayout = findView(R.id.guide_frame);
        startView = findView(R.id.layout);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        dealWithGuide();
    }

    // This is run in main thread
    private void dealWithGuide(){
        Log.v(TAG, "dealWithGuide");
        PreferenceHelper preferenceHelper = new PreferenceHelper(this);
        if(preferenceHelper.getBoolean(isNotShowGuideKeyName)){
            // normal
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    gotoNextActivity();
                }
            }, 2000);
        }else{
            // show guide frame
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(fragmentLayout.getId(), new GuideFragment());
            fragmentTransaction.commit();

            // don't set this at first
            // or you will get a white screen
            startView.setVisibility(View.GONE);
            fragmentLayout.setVisibility(View.VISIBLE);
        }
    }

    private void gotoNextActivity(){
        startActivity(new Intent(this, MainTab.class));
        finish();
    }

    public void startFromGuide(){
        Log.v(TAG, "btn enter was clicked");
        new PreferenceHelper(this).getEditor()
                .putBoolean(Login.isNotShowGuideKeyName, true)
                .apply();
        gotoNextActivity();
    }
}
